<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="cake.db.MySqlDb"%>
<%@page import="cake.dao.MessageDao"%>
<%@page import="java.sql.ResultSet"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>Message Board</title>
		<link rel="stylesheet" href="styles/style.css" type="text/css" />
		<script src="scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
		<script src="scripts/jquery.select.js" type="text/javascript"></script>
		<script src="scripts/tools.js" type="text/javascript"></script>
		<script src="scripts/script.js" type="text/javascript"></script>
	</head>
	<body>
		<div id="container">
			<div id="header">
				<%@include file="header.jsp" %>
			</div>
			<div id="content">
				 <div id="orderList">
							<table border="1" cellpadding="0" cellspacing="0" style="margin-left:100px;border-collapse: collapse;" bordercolor="#CCCCCC">
								<tr style="background-color:#FCF2E4;">
									<td width="30"> 
										Index 
									</td>
									<td width="450">
										Content
									</td>
									<td width="150">
										User
									</td>
									<td width="140">
										Time
									</td>
								</tr>
								<%
									String currPage = request.getParameter("page");
									int pageSize = 20;
									int pageNum = 1;
									if (currPage != null && !currPage.equals("")) {
										pageNum = Integer.parseInt(currPage);
									}
									MySqlDb db = new MySqlDb();

									String sql = "select count(*) from message";

									ResultSet rs = db.query(sql);

									int total = 0;
									while (rs.next()) {
										total = rs.getInt(1);
										break;
									}
									int totalPage = 0;
									if (total % pageSize == 0) {
										totalPage = total / pageSize;
									} else {
										totalPage = total / pageSize + 1;
									}

									sql = "select user.username,message.* from message "
											+ " left outer join user on message.userid=user.id"
											+ " order by occurTime asc limit "
											+ (pageNum - 1)
											* pageSize
											+ ","
											+ pageSize;

									rs = db.query(sql);
									int index = 1;
									while (rs.next()) {
								%>
								<tr>
									<td><%=index%></td>
									<td><%=rs.getString("content")%></td>
									<td><%=rs.getString("username")%></td>
									<td><%=rs.getString("occurTime")%></td>
								</tr>
								<%
									index++;
									}
								%>
							</table>
							<div>
								<%
									if (totalPage > 1) {
										for (int i = 1; i <= totalPage; i++) {
											if (i == 1) {
								%>
								<span><a href="myOrder.jsp?page=<%=i%>">[FirstPage]</a> </span>
								<%
									} else if (i == totalPage) {
								%>
								<span><a href="msgBoard.jsp?page=<%=i%>">[LastPage]</a> </span>
								<%
									} else {
								%>
								<span><a href="msgBoard.jsp?page=<%=i%>"><%=i%></a> </span>
								<%
									}
										}
									}
								%>
							</div>
							</div>
							<div>
								<table style="margin-left:200px;margin-top:20px;">
									<tr>
										<td>Leave your message:<textarea id="msg" cols="60" rows="4"></textarea></td>
									</tr>
									<tr>
										<td style="text-align:right;">
										<%
					    				if(username==null || username.equals(""))
						    				{
										%>
											<input type="button" value="" class="submit" onclick="alert('Please login first!');" />
										<%
						    				}
						    				else
						    				{
										%>
											<input type="button" value="" class="submit" onclick="leaveMsg();" />
										<%
						    				}
										%>
										</td>
									</tr>
								</table>
							</div>
				<div id="categorys">
					<%@include file="footer.jsp" %>
				</div>
			</div>
		</div>
	</body>
</html>
	